package com.kinth.work.tyc;


import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.apache.log4j.Logger;
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import com.alibaba.fastjson.JSONObject;
import com.kinth.common.HttpUtil;
import com.kinth.rest.api.CreateEmailInfoRequest;


/**
 * 1. 暂时把product和introduction放到一起在remark字段里面。
 */
public class DataPipeline implements Pipeline {

	
	private Logger logger = Logger.getLogger(DataPipeline.class);
	
	public DataPipeline() {
		super();
	}
	
	public DataPipeline(Integer subTaskId) {
		super();
	}

	//如果传入的参数为空，就输出"暂无数据"
	public String forEmpty(String s)
	{
		if(s == null)
		{
			return "暂无数据";
		}
		return s;
	}
	public void process(ResultItems resultItems, Task task) {
		//logger.info("Pipeline processes.");
		
		String forEmpty = "暂无数据";
		String name = resultItems.get("name");
		if(name == null)
		{
			return;
			//也就是如果公司名称为空则直接返回
			//name = forEmpty;
		}
			
		String product = resultItems.get("product");
		if(product == null)
		{
			product = forEmpty;
		}
		String address = resultItems.get("address");
		if(address == null)
		{
			address = forEmpty;
		}
		String people = resultItems.get("people");
		if(people == null)
		{
			people = forEmpty;
		}
		String qq = resultItems.get("qq");
		if(qq == null)
		{
		    qq = forEmpty;
		}else
		{
			qq = qq+"@qq.com";
		}
		String phone1 = resultItems.get("phone1");
		if(phone1 == null)
		{
			phone1 = forEmpty;
			
		}
		String phone2 = resultItems.get("phone2");
		if(phone2 == null)
		{
			phone2 = forEmpty;
		}
		String phone3 = resultItems.get("phone3");
		if(phone3 == null)
		{
			phone3 = forEmpty;
		}
		String introduction = resultItems.get("introduction");
		if(introduction == null)
		{
			introduction = forEmpty;
		}
		
		String industry = resultItems.get("industry");
		if(industry == null)
		{
			industry = forEmpty;
		}
		
		String source = resultItems.get("url");
		if(source == null)
		{
			source = forEmpty;
		}
		//后台传输接口

		try{

			CreateEmailInfoRequest createRequest = 
					new CreateEmailInfoRequest();
			createRequest.setAddress(address);
			createRequest.setCompany(name);
			createRequest.setEmail(qq);
			createRequest.setUsername(people);
			createRequest.setProduct(product);
			createRequest.setIntroduce(introduction);
			createRequest.setPhone1(phone1);
			createRequest.setPhone2(phone2);
			createRequest.setPhone3(phone3);
			createRequest.setIndustry(industry);
		    createRequest.setSource(source);
		    //应该是phone这里的额字段太长了。
		    

		    HttpUtil.save(createRequest);

		}catch(Exception e)
		{
			System.out.println("DataPipeline: 传输有问题"+e.toString());
			e.printStackTrace();
		}
		
		//logger.info("info:"+name+product+address+people+qq);
	}
	
	
}

